package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        //调用服务层查询数据
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }

    //新增角色
    @RequestMapping("/add/{permissionIds}")
    public Result add(@PathVariable Integer[] permissionIds, @RequestBody Role role){
        try{
            roleService.add(permissionIds,role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Permission> permissionList = roleService.findAll();
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //根据角色id查询关联的权限id数组
    @RequestMapping("/findPermissionIdsByRoleId/{roleId}")
    public List<Integer> findPermissionIdsByRoleId(@PathVariable Integer roleId){
        return roleService.findPermissionIdsByRoleId(roleId);
    }

    //修改角色权限信息
    @RequestMapping("/update/{roleId}")
    public Result update(@PathVariable Integer roleId,@RequestBody Integer[] permissionIds){
        System.out.println(roleId);
        System.out.println(permissionIds);
        try{
            roleService.update(permissionIds,roleId);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    //根据角色id删除
    @RequestMapping("/delete/{roleId}")
    public Result delete(@PathVariable Integer roleId) {
        try {
            roleService.delete(roleId);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }


    /**
     * 1.拉取所有的角色列表
     *
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        List<Role> roles = null;
        try {
            roles = roleService.getRoleList();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLE_LIST_FAIL);
        }
        return new Result(true, MessageConstant.GET_ROLE_LIST_SUCCESS, roles);

    }
}
